package com.hch.web;

import com.hch.domain.Tag;
import com.hch.service.BlogService;
import com.hch.service.TagService;
import com.hch.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;


    //获取分类id对应的博客内容
    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @PathVariable Long id, Model model) {
        //获取所有标签,设置足够大的数值就能取到所有的标签
        List<Tag> tags = tagService.listTagTop(100);
        if (id == -1) { //-1为初始值，默认从首页点击进入分类页
            id = tags.get(0).getId();
        }
        //将所有分类存入model
        model.addAttribute("tags", tags);
        //将分类的博客存入model,根据分类分页查询博客信息
        model.addAttribute("page", blogService.listBlogByTagId(id,pageable));
        //将活动的分类存入model
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
